<?php
// +----------------------------------------------------------------------
// | 吉智云科技 [ 简单 高效 卓越 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://baijiahao.codewe.cn/admin.php All rights reserved.
// +----------------------------------------------------------------------
// | Author: 下雨吧จุ๊บ <642445992@qq.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;

use Think\Cache\Driver\Db;
use Common\Controller\Controller;

/**
 * 前台默认控制器
 * @author 下雨吧จุ๊บ <642445992@qq.com>
 */
class InterfaceController extends Controller
{

    private $data;
    /**
     * 构造方法
     * @author 下雨吧จุ๊บ <642445992@qq.com>
     */
    public function __construct()
    {
        if (!empty($_POST)){
            $this->data = $_POST;
        }
        if (!empty($_GET)){
            $this->data = (object)$_GET;
        }
        if (!empty(file_get_contents('php://input'))){
            $this->data = json_decode(file_get_contents('php://input'));
        }
        if (empty($_GET) && empty($_POST) && empty(file_get_contents('php://input'))){
            echo json_encode(array(
                'status' => 0,
                'message' => '请根据接口文档正确请求',
            ));exit;
        }
    }

    /**
     * 返回json数据
     * @author 下雨吧จุ๊บ <642445992@qq.com>
     */
    public function goBack($data)
    {
        //返回json数据
        echo json_encode($data);
        exit();
    }

    /**
     * 登录验证
     * @author 下雨吧จุ๊บ <642445992@qq.com>
     */
    public function login()
    {
        // 验证用户名密码是否正确
        $user_object = D('Admin/User');
        $user_info   = $user_object->login($this->data->username, $this->data->password);
        if (!$user_info) {
            $this->goBack(array(
                'status' => 0,
                'message' => $user_object->getError()
            ));
        }
        $user = D('admin_user');
        $where->username = $this->data->username;
        $where->status = 1;
        $member = $user->where($where)->find();
        $uid = $user_object->auto_login($user_info);
        //登录成功,返回用户信息
        $this->goBack(array(
            'status' => 1,
            'message' => $member
        ));
    }

    /**
     * 获取账号列表
     * @author 下雨吧จุ๊บ <642445992@qq.com>
     */
    public function accountList()
    {
        $zhanghao = D('account');
        $where->userid = $this->data->id;
        $list = $zhanghao->where($where)->select();
        //有账号返回账号,没有返回空
        if (empty($list)){
            $this->goBack(array(
                'status' => 0,
                'message' => '对应账号没有数据源'
            ));
        }else{
            $img = D('admin_upload');
            foreach ($list as $k=>$v) {
                $map->id = $v['image'];
                $list[$k]['image'] = 'http://jzyabc.cn'.$img->where($map)->find()['path'];
            }
            $this->goBack(array(
                'status' => 1,
                'message' => $list
            ));
        }

    }

    /**
     * 获取cookie
     * @author 下雨吧จุ๊บ <642445992@qq.com>
     */
    public function cookie()
    {
        $zhanghao = D('account');
        $where->id = $this->data->id;
        $cookie = $zhanghao->where($where)->find()['cookie'];
        //有账号返回账号,没有返回空
        if (empty($cookie)){
            $this->goBack(array(
                'status' => 0,
                'message' => '对应账号没有数据源'
            ));
        }else{
            $this->goBack(array(
                'status' => 1,
                'message' => $cookie
            ));
        }
    }

    /**
     * 获取权限
     * @author 下雨吧จุ๊บ <642445992@qq.com>
     */
    public function menuList()
    {
        $id = $this->data->id;
        if ($id == 1){
            $menulist_data = $this->getMenu(0);
            foreach ($menulist_data as $key=>$v) {
                $menulist_data[$key]['id'] = $key;
                if (empty($v['grade'])){
                    $menulist_data[$key]['grade'] = 3;
                }
            }
            $menulist = $menulist_data;
        }else{
            $user_group = D('admin_access');
            $where->id = $id;
            $groupid = $user_group->where($where)->find()['id'];
            $menu = D('admin_group');
            $where->id = $groupid;
            $menulist = json_decode($menu->where($where)->find()['menu_auth'])->Admin;
            if (!empty($menulist)){
                foreach ($menulist as $k=>$v) {
                    $menulist[$k] = $this->getMenu($v);
                }
            }
        }
        if (!empty($menulist)){
            foreach ($menulist as $k=>$v) {
                if ($v['grade'] == 1){
                    $onelist[] = $v;
                }
            }
        }
        if (!empty($onelist)){
            foreach ($onelist as $k=>$v) {
                foreach ($menulist as $k1=>$v1) {
                    if ($v1['pid'] == $v['id']){
                        $onelist[$k]['child'][] = $v1;
                    }
                }
            }
        }
        $this->goBack(array(
            'status' => 1,
            'message' => $onelist
        ));
    }

    /**
     * 所有权限
     * @author 下雨吧จุ๊บ <642445992@qq.com>
     */
    public function getMenu($k)
    {
        // 模块信息配置
        $data =  array(
            // 后台菜单及权限节点配置
            'admin_menu' => array(
                '1'  => array(
                    'pid'   => '0',
                    'title' => '系统',
                    'icon'  => 'fa fa-cog',
                    'level' => 'system',
                ),
                '2'  => array(
                    'pid'   => '1',
                    'title' => '系统功能',
                    'icon'  => 'fa fa-folder-open-o',
                    'grade'  => '1',
                ),
                '3'  => array(
                    'pid'   => '2',
                    'title' => '系统设置',
                    'icon'  => 'fa fa-wrench',
                    'url'   => 'Admin/Config/group',
                    'grade'  => '2',
                ),
                '4'  => array(
                    'pid'   => '3',
                    'title' => '修改设置',
                    'url'   => 'Admin/Config/groupSave',
                ),
                '5'  => array(
                    'pid'   => '2',
                    'title' => '导航管理',
                    'icon'  => 'fa fa-map-signs',
                    'url'   => 'Admin/Nav/index',
                    'grade'  => '2',
                ),
                '6'  => array(
                    'pid'   => '5',
                    'title' => '新增',
                    'url'   => 'Admin/Nav/add',
                ),
                '7'  => array(
                    'pid'   => '5',
                    'title' => '编辑',
                    'url'   => 'Admin/Nav/edit',
                ),
                '8'  => array(
                    'pid'   => '5',
                    'title' => '设置状态',
                    'url'   => 'Admin/Nav/setStatus',
                ),
                '9'  => array(
                    'pid'   => '2',
                    'title' => '幻灯管理',
                    'icon'  => 'fa fa-image',
                    'url'   => 'Admin/Slider/index',
                    'grade'  => '2',
                ),
                '10' => array(
                    'pid'   => '9',
                    'title' => '新增',
                    'url'   => 'Admin/Slider/add',
                ),
                '11' => array(
                    'pid'   => '9',
                    'title' => '编辑',
                    'url'   => 'Admin/Slider/edit',
                ),
                '12' => array(
                    'pid'   => '9',
                    'title' => '设置状态',
                    'url'   => 'Admin/Slider/setStatus',
                ),
                '17' => array(
                    'pid'    => '2',
                    'title'  => '配置管理',
                    'icon'   => 'fa fa-cogs',
                    'url'    => 'Admin/Config/index',
                    'is_dev' => 1,
                    'grade'  => '2',
                ),
                '18' => array(
                    'pid'   => '17',
                    'title' => '新增',
                    'url'   => 'Admin/Config/add',
                ),
                '19' => array(
                    'pid'   => '17',
                    'title' => '编辑',
                    'url'   => 'Admin/Config/edit',
                ),
                '20' => array(
                    'pid'   => '17',
                    'title' => '设置状态',
                    'url'   => 'Admin/Config/setStatus',
                ),
                '21' => array(
                    'pid'   => '2',
                    'title' => '上传管理',
                    'icon'  => 'fa fa-upload',
                    'url'   => 'Admin/Upload/index',
                    'grade'  => '2',
                ),
                '22' => array(
                    'pid'   => '21',
                    'title' => '上传文件',
                    'url'   => 'Admin/Upload/upload',
                ),
                '23' => array(
                    'pid'   => '21',
                    'title' => '删除文件',
                    'url'   => 'Admin/Upload/delete',
                ),
                '24' => array(
                    'pid'   => '21',
                    'title' => '设置状态',
                    'url'   => 'Admin/Upload/setStatus',
                ),
                '25' => array(
                    'pid'   => '21',
                    'title' => '下载远程图片',
                    'url'   => 'Admin/Upload/downremoteimg',
                ),
                '26' => array(
                    'pid'   => '21',
                    'title' => '文件浏览',
                    'url'   => 'Admin/Upload/fileManager',
                ),
                '27' => array(
                    'pid'   => '1',
                    'title' => '系统权限',
                    'icon'  => 'fa fa-folder-open-o',
                    'grade'  => '1',
                ),
                '28' => array(
                    'pid'   => '27',
                    'title' => '用户管理',
                    'icon'  => 'fa fa-user',
                    'url'   => 'Admin/User/index',
                    'grade'  => '2',
                ),
                '29' => array(
                    'pid'   => '28',
                    'title' => '新增',
                    'url'   => 'Admin/User/add',
                ),
                '30' => array(
                    'pid'   => '28',
                    'title' => '编辑',
                    'url'   => 'Admin/User/edit',
                ),
                '31' => array(
                    'pid'   => '28',
                    'title' => '设置状态',
                    'url'   => 'Admin/User/setStatus',
                ),
                '32' => array(
                    'pid'   => '27',
                    'title' => '管理员管理',
                    'icon'  => 'fa fa-lock',
                    'url'   => 'Admin/Access/index',
                    'grade'  => '2',
                ),
                '33' => array(
                    'pid'   => '32',
                    'title' => '新增',
                    'url'   => 'Admin/Access/add',
                ),
                '34' => array(
                    'pid'   => '32',
                    'title' => '编辑',
                    'url'   => 'Admin/Access/edit',
                ),
                '35' => array(
                    'pid'   => '32',
                    'title' => '设置状态',
                    'url'   => 'Admin/Access/setStatus',
                ),
                '36' => array(
                    'pid'   => '27',
                    'title' => '用户组管理',
                    'icon'  => 'fa fa-sitemap',
                    'url'   => 'Admin/Group/index',
                    'grade'  => '2',
                ),
                '37' => array(
                    'pid'   => '36',
                    'title' => '新增',
                    'url'   => 'Admin/Group/add',
                ),
                '38' => array(
                    'pid'   => '36',
                    'title' => '编辑',
                    'url'   => 'Admin/Group/edit',
                ),
                '39' => array(
                    'pid'   => '36',
                    'title' => '设置状态',
                    'url'   => 'Admin/Group/setStatus',
                ),
                '40' => array(
                    'pid'   => '1',
                    'title' => '扩展中心',
                    'icon'  => 'fa fa-folder-open-o',
                    'grade'  => '1',
                ),
                '48' => array(
                    'pid'   => '40',
                    'title' => '功能模块',
                    'icon'  => 'fa fa-th-large',
                    'url'   => 'Admin/Module/index',
                    'grade'  => '2',
                ),
                '49' => array(
                    'pid'   => '48',
                    'title' => '安装检查',
                    'url'   => 'Admin/Module/install_before',
                ),
                '50' => array(
                    'pid'   => '48',
                    'title' => '安装',
                    'url'   => 'Admin/Module/install',
                ),
                '51' => array(
                    'pid'   => '48',
                    'title' => '卸载检查',
                    'url'   => 'Admin/Module/uninstall_before',
                ),
                '52' => array(
                    'pid'   => '48',
                    'title' => '卸载',
                    'url'   => 'Admin/Module/uninstall',
                ),
                '53' => array(
                    'pid'   => '48',
                    'title' => '更新信息',
                    'url'   => 'Admin/Module/updateInfo',
                ),
                '54' => array(
                    'pid'   => '48',
                    'title' => '设置状态',
                    'url'   => 'Admin/Module/setStatus',
                ),
                '55' => array(
                    'pid'   => '40',
                    'title' => '插件管理',
                    'icon'  => 'fa fa-th',
                    'url'   => 'Admin/Addon/index',
                    'grade'  => '2',
                ),
                '56' => array(
                    'pid'   => '55',
                    'title' => '安装',
                    'url'   => 'Admin/Addon/install',
                ),
                '57' => array(
                    'pid'   => '55',
                    'title' => '卸载',
                    'url'   => 'Admin/Addon/uninstall',
                ),
                '58' => array(
                    'pid'   => '55',
                    'title' => '运行',
                    'url'   => 'Admin/Addon/execute',
                ),
                '59' => array(
                    'pid'   => '55',
                    'title' => '设置',
                    'url'   => 'Admin/Addon/config',
                ),
                '60' => array(
                    'pid'   => '55',
                    'title' => '后台管理',
                    'url'   => 'Admin/Addon/adminList',
                ),
                '61' => array(
                    'pid'   => '60',
                    'title' => '新增数据',
                    'url'   => 'Admin/Addon/adminAdd',
                ),
                '62' => array(
                    'pid'   => '60',
                    'title' => '编辑数据',
                    'url'   => 'Admin/Addon/adminEdit',
                ),
                '63' => array(
                    'pid'   => '60',
                    'title' => '设置状态',
                    'url'   => 'Admin/Addon/setStatus',
                ),
            ),
        );
        if ($k == 0){
            return $data['admin_menu'];
        }
        $data['admin_menu'][$k]['id'] = $k;
        return $data['admin_menu'][$k];
    }

    public function denglu()
    {
        $username = $_GET['username'];
        $password = $_GET['password'];
        $this->redirect('Admin/Login/login');
//        $user_object = D('Admin/User');
//        $user_info   = $user_object->login($username, $password);
//        // 设置登录状态
//        $uid = $user_object->auto_login($user_info);
    }
}
